package com.atzxh.heima.netty.c1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author 小红同学
 * @date 2023-12-14-21:57
 * @description
 */
public class TestFileChannelTransferTo {
    public static void main(String[] args) {
        try (
                FileChannel from = new FileInputStream("file01.txt").getChannel();
                FileChannel to = new FileOutputStream("to.txt").getChannel()
        ) {
            // transfer效率高，底层会利用操作系统的零拷贝进行优化，最大2G数据
            // 如果大于2G，使用以下代码
            long size = from.size();
            // left代表剩余大小
            for (long left = size; left > 0;) {
                System.out.println("position: " + (size - left) + "   left: " + left);
                left -= from.transferTo((size - left), left, to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
